package com.example.form.common;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 定义返回结果
 * 属性加⼊ @JsonInclude(JsonInclude.Include.ALWAYS) 表⽰⽆论是否为空必须序列化
 * @param <T>
 */
public class AppResult <T>{
    @JsonInclude(JsonInclude.Include.ALWAYS)//不论任何时候都参与序列化
    private long code; //状态码

    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message; //状态描述

    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data; //结果数据

    // 构造⽅法
    public AppResult(long code, String message) {
        this.code = code;
        this.message = message;
    }

    public AppResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // ================= 封装常⽤⽅法 ====================
    /**
     * 成功
     */
    public static AppResult success(){
        return new AppResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }
    /**
     * 成功
     *返回的结果
     *  @param data 返回的结果
     */
    public static <T> AppResult<T> success(T data){
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }
    /**
     * 成功
     *
     * @param message ⾃定义描述
     * @param data 返回的结果
     */
    public static <T> AppResult<T> success(String message,T data){
        return new AppResult(ResultCode.SUCCESS.getCode(), message,data);
    }

    /**
     *失败
     * @return
     */
    public static AppResult failed(){
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }
    /**
     * 失败
     *
     * @param message ⾃定义描述
     */
    public static <T> AppResult<T> failed(String message){
        return new AppResult(ResultCode.FAILED.getCode(), message);
    }
    /**
     * 失败
     *
     * @param data 异常信息
     */
    public static <T> AppResult<T> failed(T data){
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(),data);
    }
    /**
     * 失败
     *
     * @param resultCode 错误状态
     */
    public static AppResult failed(ResultCode resultCode) {
        return new AppResult(resultCode.getCode(), resultCode.getMessage());
    }


    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
